import 'package:flutter/material.dart';
import 'package:loading_animation_widget/loading_animation_widget.dart';
import 'package:ducafe_ui_core/ducafe_ui_core.dart';

/// 简单全局加载蒙层
/// 只使用loading_animation_widget实现
class SimpleLoadingOverlay {
  static OverlayEntry? _overlayEntry;

  /// 显示加载蒙层
  static void show(BuildContext context, {String? message}) {
    if (_overlayEntry != null) return;

    _overlayEntry = OverlayEntry(
      builder:
          (context) => Material(
            color: Colors.black.withOpacity(0.5),
            child: Center(
              child: Container(
                padding: 20.paddingAll(),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(12.r),
                ),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    LoadingAnimationWidget.staggeredDotsWave(
                      color: Colors.blue,
                      size: 50.w,
                    ),
                    if (message != null) ...[
                      SizedBox(height: 16.h),
                      Text(
                        message,
                        style: TextStyle(
                          fontSize: 16.sp,
                          color: Colors.black87,
                        ),
                        textAlign: TextAlign.center,
                      ),
                    ],
                  ],
                ),
              ),
            ),
          ),
    );

    Overlay.of(context).insert(_overlayEntry!);
  }

  /// 隐藏加载蒙层
  static void hide() {
    _overlayEntry?.remove();
    _overlayEntry = null;
  }

  /// 检查是否正在显示
  static bool isShowing() {
    return _overlayEntry != null;
  }
}
